Right.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { GameListRep, searchGameListApi } from "@/api/home";
  2. import Card from "@/components/Card/Card";
  3. import Empty from "@/components/Empty";
  4. import { GameListTypeEnum } from "@/enums";
  5. import { debounce } from "@/utils/methods";
  6. import { InfiniteScroll } from "antd-mobile";
  7. import React from "react";
  8. import styles from "./page.module.scss";
  9. interface Props {
  10. actInfo?: { type: GameListTypeEnum; id: number } | null;
  11. }
  12. const Left: React.FC<Props> = ({ actInfo }) => {
  13. const [data, setData] = React.useState<GameListRep[]>([]);
  14. const [loading, setLoading] = React.useState<boolean>(false);
  15. const [hasMore, setHasMore] = React.useState(true);
  16. const [isInit, setIsInit] = React.useState(true);
  17. const PageRef = React.useRef({
  18. current_page: 0,
  19. page_size: 15,
  20. });
  21. React.useEffect(() => {
  22. setData([]);
  23. setHasMore(true);
  24. setLoading(false);
  25. setIsInit(true);
  26. PageRef.current.current_page = 0;
  27. }, [actInfo]);
  28. const getData = async () => {
  29. if (!actInfo?.id) return;
  30. try {
  31. setLoading(true);
  32. setIsInit(false);
  33. const params: any = {
  34. ...PageRef.current,
  35. use_page: true,
  36. };
  37. if (actInfo) {
  38. params[actInfo.type] = actInfo.id;
  39. }
  40. const res = await searchGameListApi(params);
  41. if (res.data) {
  42. const toData = [...data, ...res.data];
  43. setData(toData);
  44. if (toData.length >= res.page.total_count) {
  45. setHasMore(false);
  46. }
  47. }
  48. } finally {
  49. setLoading(false);
  50. }
  51. };
  52. const loadMore = debounce(async () => {
  53. if (!hasMore || loading || !actInfo?.id) return;
  54. PageRef.current.current_page += 1;
  55. getData();
  56. }, 500) as () => Promise<void>;
  57. return (
  58. <div className={styles.right}>
  59. <div className={styles.rightBox}>
  60. {data.map((item) => {
  61. return (
  62. <Card
  63. key={item.id}
  64. item={item}
  65. className={styles.gameCard}
  66. isShowFavorite={true}
  67. isShowOnline={true}
  68. ></Card>
  69. );
  70. })}
  71. </div>
  72. {(isInit || data.length > 0) && (
  73. <InfiniteScroll loadMore={loadMore} hasMore={hasMore}></InfiniteScroll>
  74. )}
  75. {!isInit && !loading && data.length <= 0 && <Empty></Empty>}
  76. </div>
  77. );
  78. };
  79. export default React.memo(Left);